package com.ruoyi.web.ydz.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.DoctorDisease;
import com.ruoyi.web.ydz.domain.Hospital;
import com.ruoyi.web.ydz.mapper.DoctorDiseaseMapper;
import com.ruoyi.web.ydz.mapper.HospitalMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.DoctorMapper;
import com.ruoyi.web.ydz.domain.Doctor;
import com.ruoyi.web.ydz.service.IDoctorService;

import javax.annotation.Resource;

/**
 * 医生Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class DoctorServiceImpl implements IDoctorService 
{
    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private HospitalMapper hospitalMapper;

    @Resource
    private DoctorDiseaseMapper doctorDiseaseMapper;

    /**
     * 查询医生
     * 
     * @param id 医生主键
     * @return 医生
     */
    @Override
    public Doctor selectDoctorById(Long id)
    {

        Doctor doctor = doctorMapper.selectDoctorById(id);
        if(!Objects.isNull(doctor)){
            // 获取医生详情
            if(null != doctor.getHospitalId()){
                Hospital hospital = hospitalMapper.selectHospitalById(doctor.getHospitalId());
                doctor.setHospital(hospital);
            }
            // 获取医院相关疾病
            DoctorDisease doctorDisease = new DoctorDisease();
            doctorDisease.setDoctorId(id);
            List<DoctorDisease> doctorDiseases = doctorDiseaseMapper.selectDoctorDiseaseList(doctorDisease);
            doctor.setJapanDiseaseList(doctorDiseases);
        }
        return doctor;
    }

    /**
     * 查询医生列表
     * 
     * @param doctor 医生
     * @return 医生
     *
     */
    @Override
    public List<Doctor> selectDoctorList(Doctor doctor)
    {
        return doctorMapper.selectDoctorList(doctor);
    }

    /**
     * 新增医生
     * 
     * @param doctor 医生
     * @return 结果
     */
    @Override
    public int insertDoctor(Doctor doctor)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        doctor.setCreateTime(DateUtils.getNowDate());
        doctor.setCreateBy(user.getUserName());
        doctor.setCreateName(user.getNickName());
        doctor.setUpdateBy(user.getNickName());
        doctor.setUpdateName(user.getUserName());
        doctor.setUpdateTime(DateUtils.getNowDate());
        int i = doctorMapper.insertDoctor(doctor);
        if (!Objects.isNull(doctor.getJapanDiseaseList())){
            doctor.getJapanDiseaseList().forEach(bean -> {
                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateBy(user.getUserName());
                bean.setCreateName(user.getNickName());
                bean.setDoctorId(doctor.getId());

                doctorDiseaseMapper.insertDoctorDisease(bean);
            });
        }
        return i;
    }

    /**
     * 修改医生
     * 
     * @param doctor 医生
     * @return 结果
     */
    @Override
    public int updateDoctor(Doctor doctor)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        doctor.setUpdateBy(user.getNickName());
        doctor.setUpdateName(user.getUserName());
        doctor.setUpdateTime(DateUtils.getNowDate());

        if (!Objects.isNull(doctor.getJapanDiseaseList())){
            doctor.getJapanDiseaseList().forEach(bean -> {
                if (Objects.isNull(bean.getId())){
                    bean.setCreateTime(DateUtils.getNowDate());
                    bean.setCreateBy(user.getUserName());
                    bean.setCreateName(user.getNickName());
                    bean.setDoctorId(doctor.getId());

                    doctorDiseaseMapper.insertDoctorDisease(bean);
                }else{
                    doctorDiseaseMapper.updateDoctorDisease(bean);
                }
            });
        }

        return doctorMapper.updateDoctor(doctor);
    }

    /**
     * 批量删除医生
     * 
     * @param ids 需要删除的医生主键
     * @return 结果
     */
    @Override
    public int deleteDoctorByIds(Long[] ids)
    {
        return doctorMapper.deleteDoctorByIds(ids);
    }

    /**
     * 删除医生信息
     * 
     * @param id 医生主键
     * @return 结果
     */
    @Override
    public int deleteDoctorById(Long id)
    {
        return doctorMapper.deleteDoctorById(id);
    }
}
